<?php
/**
 * 检测权限校验行为钩子
 */

namespace app\admin\behavior;


use think\auth\Auth;
use think\Config;
use think\helper\Str;
use think\Request;
use think\View;
load_trait('controller/Jump');

class AuthBehavior
{
    use \traits\controller\Jump ;//类里面引入jump;类
    public function moduleInit(&$params)
    {
        //获取当前模块

        $request = Request::instance();
        $module = $request->module();
        $controller = $request->controller();
        $action = $request->action();
        $url = Str::lower($module . DS . $controller . DS . $action);
        $url = str_replace("\\", "/", $url);

        $auth = Auth::instance();
        $config = Config::get('login_admin');

        //获取无需认证模块配置
        $modules = explode(',', $config['not_auth_module']);

        if (!in_array(Str::lower($controller), $modules) && !$auth->check($url, session($config['auth_uid']))) {
            if ($request->isAjax()) {
                $this->redirect($config['no_permiss']);
            } else {
                $this->redirect($config['no_permiss']);
            }
        }

    }
}